package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.bean.Orders;
import com.woniu.bean.User;
import com.woniu.bean.UserBO;
import org.apache.ibatis.annotations.*;

import java.io.Serializable;
import java.util.List;


@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Select("select user_id as id , user_name as name from `users` where user_id = #{id}")
    @Results({

                    // 构建订单 某个用户的id可以取到它对应的订单
                   /* @Result(property = "orders" , column = "id" ,
                            many = @Many(select = "findOrdersByUserId")),*/
                    @Result(property = "id" , column = "id")
//                    @Result(column = "user_id" , property = "id"),              // 可写可不写，但是有要求。 如果不写的话，列名和属性对应上。
//                    @Result(column = "user_name" , property = "name"),          // 可写可不写，但是有要求。 如果不写的话，列名和属性对应上。
            })
    User findUser(@Param("id") Long id);


    @Select("select * from orders where user_id = #{userId}")
    @Results({
//            @Result(property = "user" , column = "user_id", one = @One(select = "findUserById"))
    })
    List<Orders> findOrdersByUserId(@Param("userId") Long userId);

    @Select("select user_id as id , user_name as name from `users` where user_id = #{id}")
    User findUserById(@Param("id") Long id);

    // 构建一个BO对象
     // 目标：一个sql语句把这个BO对象全部构建完成
    @Select("select u.user_id,u.user_name,u.user_email,\n" +
            "\t\t\to.oid,o.money,o.accepter  from users u\n" +
            "inner join orders o on (u.user_id = o.user_id) where u.user_id = 1")
    @Results(
                // 含义：user对象的userId属性和列名user_id对应上
            @Result(property = "user.id" , column = "user_id")
    )
    UserBO findUserBOById(@Param("id") Long id);
}
